package com.mxw.order.controller;

import com.mxw.account.dto.AccountDTO;
import com.mxw.account.feign.AccountFeign;
import com.mxw.commons.restful.Result;
import com.mxw.order.domain.Order;
import com.mxw.order.service.OrderService;
import com.mxw.order.dto.OrderDTO;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description TODO
 * @Author RingKo
 * @Date 2021/8/25 15:48
 * @Version 1.0
 */
@Slf4j
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AccountFeign accountFeign;

    @GlobalTransactional
    @PostMapping("order/insert")
    public Result insert(@RequestBody OrderDTO orderDTO) {
        log.info("insert order,orderDTO is :{}", orderDTO);
        //本地事务
        orderService.save(Order.builder()
                .orderNo(orderDTO.getOrderNo())
                .accountCode(orderDTO.getAccountCode())
                .productCode(orderDTO.getProductCode())
                .count(orderDTO.getCount())
                .amount(orderDTO.getAmount())
                .build());
        //feign调用
        return accountFeign.insert(AccountDTO.builder()
                    .accountCode(orderDTO.getAccountCode())
                    .amount(orderDTO.getAmount())
                    .build());
    }
}
